import axios, {Method} from "axios";
import {showToast} from 'vant';

const service = axios.create({
    baseURL: '/api',
    timeout: 10000
})
service.interceptors.response.use(response => {
        if (response.status !== 200) {
            showToast({
                icon: 'error',
                message: response.data.msg
            })
        } else if (response.data.msg) {
            showToast({
                icon: 'success',
                message: response.data.msg
            })
        }
        return Promise.resolve(response.data)
    },
    (res) => {
        showToast(res.response.data.msg)
        return Promise.reject()
    })

export const request = (method: Method, url: string, data: any = {}) => {
    return service({
        method,
        headers: {
            "Content-Type": 'application/json',
            "auth": localStorage.getItem('token') || ''
        },
        url,
        data
    }) as Promise<any>
}